package com.karson.admin.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.karson.common.model.BehaviorWeight;
import com.karson.common.request.AlbumSearchRequest;
import com.karson.common.request.PageParamRequest;
import com.karson.common.response.AlbumResponse;
import com.karson.common.response.CommonResult;
import com.karson.service.service.BehaviorWeightService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author ZKS
 */
@RestController
@RequestMapping("/api/action")
@Tag(name = "用户行为管理接口")
public class UserActionController {
    @Autowired
    private BehaviorWeightService weightService;
    @Operation(summary = "分页列表")
    @GetMapping("/list")
    public CommonResult<IPage<BehaviorWeight>> getList(@ModelAttribute @ParameterObject PageParamRequest pageParamRequest) {
        IPage<BehaviorWeight> behaviorWeightPage = new Page<>(pageParamRequest.getCurrent(), pageParamRequest.getSize());
        IPage<BehaviorWeight> pageInfo =  weightService.pageList(behaviorWeightPage);
        return CommonResult.success(pageInfo);
    }
    @Operation(summary = "回显表单")
    @GetMapping("/getBehaviorById")
    public CommonResult<BehaviorWeight> getBehaviorById(@RequestParam Integer id) {
        if (id == null || id <= 0) {return CommonResult.failed();}
        BehaviorWeight behaviorWeight = weightService.getById(id);
        return CommonResult.success(behaviorWeight);
    }
    @Operation(summary = "修改信息")
    @PostMapping("update")
    public CommonResult<String> update(@Validated @RequestBody BehaviorWeight behaviorWeight) {
        boolean update = weightService.updateById(behaviorWeight);
        if (!update) {
            return CommonResult.failed();
        }
        return CommonResult.success();
    }
    @Operation(summary = "删除配置")
    @DeleteMapping("/del")
    public CommonResult<String> delete(@Validated @RequestBody BehaviorWeight behaviorWeight) {
        LambdaUpdateWrapper<BehaviorWeight> updateWrapper = new LambdaUpdateWrapper<BehaviorWeight>().eq(BehaviorWeight::getId, behaviorWeight.getId()).set(BehaviorWeight::getIsDel, true);
        boolean update = weightService.update(updateWrapper);
        if (!update) {
            return CommonResult.failed();
        }
        return CommonResult.success();
    }
    @Operation(summary = "添加配置")
    @PostMapping("/add")
    public CommonResult<String> add(@Validated @RequestBody BehaviorWeight behaviorWeight) {
        boolean add = weightService.save(behaviorWeight);
        if (!add) {
            return CommonResult.failed();
        }
        return CommonResult.success();
    }
}
